/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Teachsubsection;
import me.zhengjie.modules.system.domain.TeachsubsectionDo;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.TeachsubsectionService;
import me.zhengjie.modules.system.domain.vo.TeachsubsectionQueryCriteria;
import me.zhengjie.modules.system.mapper.TeachsubsectionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.system.domain.TeachsubsectionDo.convertFromTypeA;

/**
* @description 服务实现
* @author shitou
* @date 2024-04-22
**/
@Service
@RequiredArgsConstructor
public class TeachsubsectionServiceImpl extends ServiceImpl<TeachsubsectionMapper, Teachsubsection> implements TeachsubsectionService {

    private final TeachsubsectionMapper teachsubsectionMapper;

    @Override
    public PageResult<Teachsubsection> queryAll(TeachsubsectionQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(teachsubsectionMapper.findAll(criteria, page));
    }

    @Override
    public List<Teachsubsection> queryAll(TeachsubsectionQueryCriteria criteria){
        return teachsubsectionMapper.findAll(criteria);
    }
    @Override
    public List<TeachsubsectionDo> querySubsectionList(Integer tenantid){
        List<Teachsubsection> teachsubsectionList = teachsubsectionMapper.querySubsectionList(tenantid);
        List<TeachsubsectionDo> list1 = new ArrayList<>();
        for(Teachsubsection teachsubsection :teachsubsectionList){
            list1.add( convertFromTypeA(teachsubsection));
        }

        return list1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(Teachsubsection resources) {
        save(resources);
        return resources.getId();
    }

    @Override
    public Teachsubsection queryTeachsubsectionMsg(Integer teachsubsectionid){
        Teachsubsection teachsubsection = getById(teachsubsectionid);
        return teachsubsection;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long update(Teachsubsection resources) {
        Teachsubsection teachsubsection = getById(resources.getId());
        teachsubsection.copy(resources);
        saveOrUpdate(teachsubsection);
        return teachsubsection.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Teachsubsection> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Teachsubsection teachsubsection : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("数字教材", teachsubsection.getTeachid());
            map.put("章节", teachsubsection.getTeachchapterid());
            map.put("根", teachsubsection.getRootid());
            map.put("名称", teachsubsection.getName());
            map.put("内容", teachsubsection.getContent());
            map.put("状态", teachsubsection.getState());
            map.put("排序", teachsubsection.getOrderBy());
            map.put("创作者", teachsubsection.getUserid());
            map.put("创作时间", teachsubsection.getCreatetime());
            map.put("操作时间", teachsubsection.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}